﻿using System;
using System.Collections.Generic;
using System.Text;
using Y.Common;
using System.Linq;
namespace RedisInAction
{
    public class Queue
    {
        string key = Y.Common.RandomString.CreateIDString();

        public Queue(String QueueName)
        {
            if (!QueueName.IsVoid())
            {
                key = $"{QueueName}:Queue";
            }
        }

        public long Count
        {
            get
            {
                return RedisService.RedisFactory.DefaultRedis.LLen(key);
            }
        }

        /// <summary>
        /// 返回队列第一个元素，并移除
        /// </summary>
        /// <returns></returns>
        public string Dequeue()
        {
            return RedisService.RedisFactory.DefaultRedis.RPop(key);
        }
        /// <summary>
        /// 插入一个元素
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public long Enqueue(string item)
        {
            return RedisService.RedisFactory.DefaultRedis.LPush(key, item);
        }
        /// <summary>
        /// 返回队列第一个元素，不会移除
        /// </summary>
        /// <returns></returns>
        public string Peek()
        {
            return RedisService.RedisFactory.DefaultRedis.LRange(key, 0, 0).FirstOrDefault();
        }

        public void Clear()
        {
            RedisService.RedisFactory.DefaultRedis.Del(key);
        }
    }
}
